<template>
  <div>
    <el-form
      :inline="true"
      :model="searchMap"
      ref="searchMap"
      class="demo-form-inline"
    >
      <el-form-item prop="cardNum">
        <el-input v-model="searchMap.cardNum" placeholder="会员卡号"></el-input>
      </el-form-item>
      <el-form-item prop="name">
        <el-input v-model="searchMap.name" placeholder="会员名称"></el-input>
      </el-form-item>
      <el-form-item prop="phone">
        <el-input v-model="searchMap.phone" placeholder="手机号码"></el-input>
      </el-form-item>
      <el-form-item prop="payType">
        <el-select v-model="searchMap.payType" placeholder="支付类型">
          <el-option
            v-for="payType in payTypeOptions"
            :key="payType.id"
            :label="payType.name"
            value="payType.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="birthday">
        <el-date-picker
          value-format="yyyy-MM-dd"
          v-model="searchMap.birthday"
          type="date"
          placeholder="出生日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="fencthData">查询</el-button>
        <el-button type="primary" @click="handleAddMember('addMemeberForm')"
          >新增</el-button
        >
        <el-button @click="resetForm('searchMap')">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="members" height="550" border style="width: 100%">
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column prop="cardNum" label="会员卡号"></el-table-column>
      <el-table-column prop="name" label="会员姓名"></el-table-column>
      <el-table-column prop="birthday" label="会员生日"></el-table-column>
      <el-table-column prop="phone" label="手机号码"></el-table-column>
      <el-table-column prop="integral" label="可用积分"></el-table-column>
      <el-table-column prop="money" label="开卡金额"></el-table-column>
      <el-table-column prop="payType" label="支付类型">
        <template slot-scope="scope">
          <span>{{ scope.row.payType | payFileter }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="会员地址"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row.id, 'addMemeberForm')"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <el-dialog
      :title="formTitle"
      :visible.sync="dialogFormVisible"
      width="500px"
    >
      <el-form
        ref="addMemeberForm"
        label-width="100px"
        label-position="right"
        style="width:400px"
        :rules="rules"
        :model="newMemberInfo"
      >
        <el-form-item label="会员卡号" prop="cardNum">
          <el-input v-model="newMemberInfo.cardNum"></el-input>
        </el-form-item>
        <el-form-item label="会员姓名" prop="name">
          <el-input v-model="newMemberInfo.name"></el-input>
        </el-form-item>
        <el-form-item label="会员生日" prop="birthday">
          <el-date-picker
            value-format="yyyy-MM-dd"
            v-model="newMemberInfo.birthday"
            type="date"
            placeholder="出生日期"
            prop="birthday"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="newMemberInfo.phone"></el-input>
        </el-form-item>
        <el-form-item label="可用积分" prop="integral">
          <el-input v-model="newMemberInfo.integral"></el-input>
        </el-form-item>
        <el-form-item label="开卡金额" prop="money">
          <el-input v-model="newMemberInfo.money"></el-input>
        </el-form-item>
        <el-form-item label="支付方式" prop="payType">
          <el-select
            v-model="newMemberInfo.payType"
            placeholder="请选择支付类型"
          >
            <el-option
              v-for="payType in payTypeOptions"
              :key="payType.id"
              :label="payType.name"
              value="payType.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="会员地址" prop="address">
          <el-input type="textarea" v-model="newMemberInfo.address"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancleForm('addMemeberForm')">取 消</el-button>
        <!-- <el-button type="primary" @click="addMember('addMemeberForm')">确 定</el-button> -->
        <el-button
          type="primary"
          @click="
            newMemberInfo.id === ''
              ? addMember('addMemeberForm')
              : updateMember('addMemeberForm')
          "
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import MemberApi from "@/api/member";
const payTypeOptions = [
  { id: "1", name: "现金" },
  { id: "2", name: "微信" },
  { id: "3", name: "支付宝" },
  { id: "4", name: "银行卡" }
];
export default {
  data() {
    return {
      members: [],
      total: 0,
      currentPage: 1,
      pageSize: 10,
      formTitle: "新增会员 ",
      searchMap: {
        cardNum: "",
        name: "",
        phone: "",
        payType: "",
        birthday: "",
        integral: "",
        money: "",
        address: ""
      },
      payTypeOptions,
      dialogFormVisible: false,
      newMemberInfo: {
        id: "",
        cardNum: "",
        name: "",
        phone: "",
        payType: "",
        birthday: "",
        integral: "",
        money: "",
        address: ""
      },
      rules: {
        cardNum: [
          { required: true, message: "请输入会员卡号", trigger: "blur" }
        ],
        name: [{ required: true, message: "请输入会员姓名", trigger: "blur" }],
        payType: [
          { required: true, message: "请输入支付方式", trigger: "change" }
        ]
      }
    };
  },
  methods: {
    fencthData() {
      // MemberApi.getList().then(response => {
      MemberApi.search(this.currentPage, this.pageSize, this.searchMap).then(
        response => {
          console.log(response.data);
          const respData = response.data;
          if (respData.flag) {
            this.total = respData.data.total;
            this.members = respData.data.rows;
            console.log(this.members);
          } else {
            this.$message({
              message: response.data.message,
              type: "warning",
              duration: 1000
            });
          }
        }
      );
    },
    handleEdit(id, formName) {
      this.dialogFormVisible = true;
      this.formTitle = "会员编辑";
      this.$nextTick(() => {
        this.$refs[formName].resetFields();
      });
      MemberApi.getById(id).then(response => {
        if (response.data.flag) {
          this.newMemberInfo = response.data.data;
          console.log(this.newMemberInfo);
        } else {
          this.$message({
            message: response.data.message,
            type: "warning",
            duration: 1000
          });
        }
      });
    },
    handleDelete(id) {
      console.log(id);
      this.$confirm("此操作将删除该会员信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          MemberApi.delById(id).then(response => {
            if (response.data.flag) {
              this.fencthData();
              this.$message({
                type: "success",
                message: "删除成功!"
              });
            } else {
              this.$message({
                message: response.data.message,
                type: "warning",
                duration: 1000
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.fencthData();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.fencthData();
    },
    resetForm(formName) {
      console.log("resetForm:", formName);
      this.$refs[formName].resetFields();
    },
    cancleForm(formName) {
      console.log("cancleForm:", formName);
      this.$refs[formName].resetFields();
      this.dialogFormVisible = false;
    },
    handleAddMember(formName) {
      this.dialogFormVisible = true;
      this.formTitle = "新增会员";
      this.$nextTick(() => {
        this.$refs[formName].resetFields();
      });
      // this.$refs[formName].resetFields();
    },
    addMember(formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) {
          return;
        } else {
          console.log("resetForm:", formName);
          MemberApi.addMember(this.newMemberInfo).then(response => {
            console.log(response.data);
            const respData = response.data;
            if (respData.flag) {
              this.$message({
                message: `添加会员Success`,
                type: "success",
                duration: 1000
              });
              this.dialogFormVisible = false;
              this.$refs[formName].resetFields();
              this.fencthData();
            } else {
              this.$message({
                message: response.data.message,
                type: "warning",
                duration: 1000
              });
            }
          });
        }
      });

      // this.$refs[formName].resetFields();
    },
    updateMember(formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) {
          return;
        } else {
          console.log("resetForm:", formName);
          MemberApi.updateMember(this.newMemberInfo).then(response => {
            console.log(response.data);
            const respData = response.data;
            if (respData.flag) {
              this.$message({
                message: `会员更新Success`,
                type: "success",
                duration: 1000
              });
              this.dialogFormVisible = false;
              this.$refs[formName].resetFields();
              this.fencthData();
            } else {
              this.$message({
                message: response.data.message,
                type: "warning",
                duration: 1000
              });
            }
          });
        }
      });

      // this.$refs[formName].resetFields();
    }
  },
  filters: {
    payFileter(typeId) {
      // const payType = payTypeOptions.find(obj => {
      //   obj.id === typeId
      // })
      const payType = payTypeOptions.find(obj => obj.id === typeId);
      return payType ? payType.name : null;
    }
  },
  created() {
    this.fencthData();
  }
};
</script>
<style scoped>
.el-form {
  margin-top: 20px;
}
</style>
